// pages/report/report.js
var app = getApp()
var total = [];
var tempFilePaths;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imageUrl: [],
        _num:3,
        suggestsList:[],
        objectArray: [
            {
              id: 0,
              title: '摄像头故障',
              content:"已解决",
              date: '2021-5-1',
              imagesmall:"https://z3.ax1x.com/2021/05/06/glibuD.jpg",
            },
            {
              id:1,
              title: '邪教宣传',
              content:"已上报",
              date: '2021-5-1',
              imagesmall:"https://z3.ax1x.com/2021/05/06/gliTgK.jpg"
            },
          ],

    },


    // 选择图片
  chooseImg: function(){
    var that = this;
    wx.chooseImage({
      count: 3, 
      sizeType: ['original', 'compressed'], 
      sourceType: ['album', 'camera'],
      success: function (res) {
        tempFilePaths = res.tempFilePaths
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.uploadFile2(tempFilePaths, 0);
        var uploadedImageLists = that.data.imageUrl;
        var totalImages = uploadedImageLists.concat(res.tempFilePaths);
        var aNum = 0;
        if (totalImages.length >= 3) {
          totalImages.length = 3;
          aNum = 1;
        }
        that.setData({
          num: 0,
          addNum: aNum,
          imageUrl: totalImages
        })
      }
    })
  },
  uploadFile2: function (file, i) {//递归调用
    var that = this;
    wx.uploadFile({
      url: app.getHeader() +'/SheQu/uploadWxImg', //仅为
      filePath: file[i],
      name: 'img',
      success: function (res) {
        var obj = new Object();
        obj.ind = i + 1;
        var data = res.data;
        console.info(data);
        obj.src = data;
        console.info("--------------*****-------------------");
        console.info(obj);
        if (!((i + 1) == file.length)) {
          total.push(obj);
          that.uploadFile2(file, i + 1);
        } else {
          total.push(obj);
          that.setData({ perImgSrc: total });
        }
      }
    })
  },
  removeImage: function(e){
    var that = this;
    var currentImageLists = that.data.imageUrl; 
    currentImageLists.splice(e.target.dataset.index,1);
    that.setData({
        imageUrl:currentImageLists
    })
    if(currentImageLists.length < 3 && currentImageLists.length >= 1) {
       that.setData({
            addNum: 0
       })
    } else if(currentImageLists.length == 0) {
       that.setData({
            addNum: 1,
            num: 1
       })
    }
  },
  preImage:function(e){
    var that = this;
    var currentImageUrl = that.data.imageUrl[e.target.dataset.index]
    wx.previewImage({
      current: currentImageUrl, // 当前显示图片的http链接
      urls: that.data.imageUrl // 需要预览的图片http链接列表
    })
  },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    bindFormSubmit: function(e) {
        console.log(e.detail.value.textarea)
    },
    driver_two_click: function () {
        var that = this
        wx.showActionSheet({
          itemList: ['拍照', '从相册中选择'],
          success(res) {
            console.log(res.tapIndex)
            let sourceType = 'camera'
            if (res.tapIndex == 0) {
              sourceType = 'camera'
            } else if (res.tapIndex == 1) {
              sourceType = 'album'
            }
            wx.chooseImage({
              count: 1,
              sizeType: ['original', 'compressed'],
              sourceType: [sourceType],
              success: function (res) {
               
              },
            })
          },
        })
      },
})